package com.cooud.gateway.predicates.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.lang.annotation.Annotation;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2025/03/19/14:08
 * @Description:
 */
//全局过滤器
@Component
@Slf4j
public class RtGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        long startTime = System.currentTimeMillis();
        log.info("请求【{}】开始: 请求时间{}",exchange.getRequest().getURI(),startTime);
//        以上是前置逻辑
        Mono<Void> filter = chain.filter(exchange)  // 放行请求    异步处理
//                 以下是后置逻辑
                .doFinally((result)->{
                    log.info("请求【{}】结束: 请求时间{}，执行时间{}ms",exchange.getRequest().getURI(),System.currentTimeMillis(),System.currentTimeMillis()-startTime);
                });
        return filter;
    }

//    执行顺序 数字越小，优先级越高
    @Override
    public int getOrder() {
        return 0;
    }
}
